#include "facechange.h"
#include "ui_facechange.h"
#include <QPropertyAnimation>
#include <QParallelAnimationGroup>
#include <QGraphicsOpacityEffect>
#include <QFileDialog>
#include <QDesktopServices>
#include "myclicklabel.h"
#include "QNetworkRequest"
#include "QNetworkReply"
#include "QJsonParseError"
#include "QJsonArray"
#include "QJsonDocument"
#include "QJsonObject"
#include "QJsonValueRef"
#include "QDebug"

faceChange::faceChange(QWidget *parent, Person * m_user) :
    QWidget(parent),
    ui(new Ui::faceChange),
    face_user(m_user)
{
    ui->setupUi(this);

    basicSettings();
    connectSettings();
}

faceChange::~faceChange()
{
    delete ui;
}

void faceChange::basicSettings()
{
    //设置图片框居中
    ui->labPixmap101->setAlignment(Qt::AlignCenter);
    ui->labPixmap102->setAlignment(Qt::AlignCenter);
    ui->labPixmap201->setAlignment(Qt::AlignCenter);
    ui->labPixmap202->setAlignment(Qt::AlignCenter);
    ui->labPixmap301->setAlignment(Qt::AlignCenter);
    ui->labPixmap302->setAlignment(Qt::AlignCenter);

    //按钮初始不可点击
    ui->btnResult1->setEnabled(false);
    ui->btnResult1->setStyleSheet("QPushButton{background: transparent;font: 13pt '黑体';color:rgb(240, 240, 240);border-radius:15px;background-color:rgb(128, 128, 128);}");
    ui->btnResult2->setEnabled(false);
    ui->btnResult2->setStyleSheet("QPushButton{background: transparent;font: 13pt '黑体';color:rgb(240, 240, 240);border-radius:15px;background-color:rgb(128, 128, 128);}");
    ui->btnResult3->setEnabled(false);
    ui->btnResult3->setStyleSheet("QPushButton{background: transparent;font: 13pt '黑体';color:rgb(240, 240, 240);border-radius:15px;background-color:rgb(128, 128, 128);}");
}

void faceChange::connectSettings()
{
    //换页功能（动画效果需在这里设置）
    connect(ui->btnLeft1, &QPushButton::clicked, [=](){
        ui->stackedWidget->setCurrentIndex(2);
    });
    connect(ui->btnRight1, &QPushButton::clicked, [=](){
        ui->stackedWidget->setCurrentIndex(1);
    });

    connect(ui->btnLeft2, &QPushButton::clicked, [=](){
        ui->stackedWidget->setCurrentIndex(0);
    });
    connect(ui->btnRight2, &QPushButton::clicked, [=](){
        ui->stackedWidget->setCurrentIndex(2);
    });

    connect(ui->btnLeft3, &QPushButton::clicked, [=](){
        ui->stackedWidget->setCurrentIndex(1);
    });
    connect(ui->btnRight3, &QPushButton::clicked, [=](){
        ui->stackedWidget->setCurrentIndex(0);
    });

    //各上传文件按钮连接
    connect(ui->btnPixmap101, &QPushButton::clicked, this, &faceChange::btnPixmap101_clicked);
    connect(ui->btnPixmap102, &QPushButton::clicked, this, &faceChange::btnPixmap102_clicked);
    connect(ui->btnPixmap201, &QPushButton::clicked, this, &faceChange::btnPixmap201_clicked);
    connect(ui->btnPixmap202, &QPushButton::clicked, this, &faceChange::btnPixmap202_clicked);
    connect(ui->btnPixmap301, &QPushButton::clicked, this, &faceChange::btnPixmap301_clicked);
    connect(ui->btnPixmap302, &QPushButton::clicked, this, &faceChange::btnPixmap302_clicked);
    connect(ui->btnResult1, &QPushButton::clicked, this, &faceChange::btnResult1_clicked);
    connect(ui->btnResult2, &QPushButton::clicked, this, &faceChange::btnResult2_clicked);
    connect(ui->btnResult3, &QPushButton::clicked, this, &faceChange::btnResult3_clicked);
}

void faceChange::btnPixmap101_clicked()
{
    imgPath101 = QFileDialog::getOpenFileName(this, "Open File", "E:\\Python\\FinalAIface\\test", "Image File(*.jpg;*.png;*.jfif)");

    if(!imgPath101.isNull())
    {
        ui->labPixmap101->setPixmap(QPixmap(imgPath101).scaled(ui->labPixmap101->width(), ui->labPixmap101->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
    }
    if(!imgPath101.isNull() && !imgPath102.isNull())
    {
        ui->btnResult1->setEnabled(true);
        ui->btnResult1->setStyleSheet("QPushButton{background: transparent;font: 13pt '黑体';color:rgb(240, 240, 240);border-radius:15px;background-color:rgb(255, 0, 0);}"
                                     "QPushButton:hover{background-color:rgb(220, 0, 0);}"
                                     "QPushButton:pressed{background-color:rgb(200, 0, 0);}");
    }
}

void faceChange::btnPixmap102_clicked()
{
    imgPath102 = QFileDialog::getOpenFileName(this, "Open File", "E:\\Python\\FinalAIface\\test", "Image File(*.jpg;*.png;*.jfif)");

    if(!imgPath102.isNull())
    {
        ui->labPixmap102->setPixmap(QPixmap(imgPath102).scaled(ui->labPixmap102->width(), ui->labPixmap102->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
    }
    if(!imgPath101.isNull() && !imgPath102.isNull())
    {
        ui->btnResult1->setEnabled(true);
        ui->btnResult1->setStyleSheet("QPushButton{background: transparent;font: 13pt '黑体';color:rgb(240, 240, 240);border-radius:15px;background-color:rgb(255, 0, 0);}"
                                     "QPushButton:hover{background-color:rgb(220, 0, 0);}"
                                     "QPushButton:pressed{background-color:rgb(200, 0, 0);}");
    }
}

void faceChange::btnPixmap201_clicked()
{
    imgPath201 = QFileDialog::getOpenFileName(this, "Open File", "E:\\Python\\FinalAIface\\test", "Image File(*.jpg;*.png;*.jfif)");

    if(!imgPath201.isNull())
    {
        ui->labPixmap201->setPixmap(QPixmap(imgPath201).scaled(ui->labPixmap201->width(), ui->labPixmap201->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
    }
    if(!imgPath201.isNull() && !imgPath202.isNull())
    {
        ui->btnResult2->setEnabled(true);
        ui->btnResult2->setStyleSheet("QPushButton{background: transparent;font: 13pt '黑体';color:rgb(240, 240, 240);border-radius:15px;background-color:rgb(255, 0, 0);}"
                                     "QPushButton:hover{background-color:rgb(220, 0, 0);}"
                                     "QPushButton:pressed{background-color:rgb(200, 0, 0);}");
    }
}

//处理返回的换脸图片
void faceChange::dealMsg(QNetworkReply *reply)
{
    QByteArray array = reply->readAll();
    QJsonParseError error;
    QJsonDocument data = QJsonDocument::fromJson(array,&error);
    if(!data.isNull())
    {
        QJsonArray obj = data.array();
        QJsonObject object = obj[0].toObject();

        resultPath1 = object.value("path").toString();
        resultPath1 = QString("file:///") + resultPath1;
        QDesktopServices::openUrl(QUrl(resultPath1));
    }
    emit closeWait();
}

//处理返回的视频封面
void faceChange::dealMsg1(QNetworkReply *reply)
{
    QByteArray array = reply->readAll();
    QJsonParseError error;
    QJsonDocument data = QJsonDocument::fromJson(array,&error);
    if(!data.isNull())
    {
        QJsonArray obj = data.array();
        QJsonObject object = obj[0].toObject();

        imgPath202 = object.value("cover").toString();

        if(!imgPath202.isNull())
        {
            ui->labPixmap202->setPixmap(QPixmap(imgPath202).scaled(ui->labPixmap202->width(), ui->labPixmap202->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
        }
        if(!imgPath201.isNull() && !imgPath202.isNull())
        {
            ui->btnResult2->setEnabled(true);
            ui->btnResult2->setStyleSheet("QPushButton{background: transparent;font: 13pt '黑体';color:rgb(240, 240, 240);border-radius:15px;background-color:rgb(255, 0, 0);}"
                                         "QPushButton:hover{background-color:rgb(220, 0, 0);}"
                                         "QPushButton:pressed{background-color:rgb(200, 0, 0);}");
        }
    }
}
//301
void faceChange::dealMsg2(QNetworkReply *reply)
{
    QByteArray array = reply->readAll();
    QJsonParseError error;
    QJsonDocument data = QJsonDocument::fromJson(array,&error);
    if(!data.isNull())
    {
        QJsonArray obj = data.array();
        QJsonObject object = obj[0].toObject();

        imgPath301 = object.value("cover").toString();

        if(!imgPath301.isNull())
        {
            ui->labPixmap301->setPixmap(QPixmap(imgPath301).scaled(ui->labPixmap301->width(), ui->labPixmap301->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
        }
        if(!imgPath301.isNull() && !imgPath302.isNull())
        {
            ui->btnResult3->setEnabled(true);
            ui->btnResult3->setStyleSheet("QPushButton{background: transparent;font: 13pt '黑体';color:rgb(240, 240, 240);border-radius:15px;background-color:rgb(255, 0, 0);}"
                                         "QPushButton:hover{background-color:rgb(220, 0, 0);}"
                                         "QPushButton:pressed{background-color:rgb(200, 0, 0);}");
        }
    }
}
//302
void faceChange::dealMsg3(QNetworkReply *reply)
{
    QByteArray array = reply->readAll();
    QJsonParseError error;
    QJsonDocument data = QJsonDocument::fromJson(array,&error);
    if(!data.isNull())
    {
        QJsonArray obj = data.array();
        QJsonObject object = obj[0].toObject();

        imgPath302 = object.value("cover").toString();

        if(!imgPath302.isNull())
        {
            ui->labPixmap302->setPixmap(QPixmap(imgPath302).scaled(ui->labPixmap302->width(), ui->labPixmap302->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
        }
        if(!imgPath301.isNull() && !imgPath302.isNull())
        {
            ui->btnResult3->setEnabled(true);
            ui->btnResult3->setStyleSheet("QPushButton{background: transparent;font: 13pt '黑体';color:rgb(240, 240, 240);border-radius:15px;background-color:rgb(255, 0, 0);}"
                                         "QPushButton:hover{background-color:rgb(220, 0, 0);}"
                                         "QPushButton:pressed{background-color:rgb(200, 0, 0);}");
        }
    }
}
//处理返回的换脸视频
void faceChange::dealMsg4(QNetworkReply *reply)
{
    QByteArray array = reply->readAll();
    QJsonParseError error;
    QJsonDocument data = QJsonDocument::fromJson(array,&error);
    if(!data.isNull())
    {
        QJsonArray obj = data.array();
        QJsonObject object = obj[0].toObject();

        resultPath2 = object.value("path").toString();

        qDebug() << resultPath2;

        resultPath2 = QString("file:///") + resultPath2;
        QDesktopServices::openUrl(QUrl(resultPath2));
    }
    emit closeWait();
}

//请求视频封面
void faceChange::btnPixmap202_clicked()
{
    videoPath202 = QFileDialog::getOpenFileName(this, "Open File", "E:\\Python\\FinalAIface\\test", "Image File(*.mp4;*.avi)");

    //请求视频封面
    // URL
    QString baseUrl = "http://127.0.0.1:5000/VideoDepart?Vpath=" + videoPath202;
    QUrl url(baseUrl);

    // 构造请求QNetworkRequest
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    request.setUrl(url);

    QNetworkAccessManager *manager = new QNetworkAccessManager();

    // 发送get请求
    manager->get(request);

    connect(manager, SIGNAL(finished(QNetworkReply*)), this,SLOT(dealMsg1(QNetworkReply*)));
}

void faceChange::btnPixmap301_clicked()
{
    videoPath301 = QFileDialog::getOpenFileName(this, "Open File", "E:\\Python\\FinalAIface\\test", "Image File(*.mp4;*.avi)");

    //请求视频封面
    // URL
    QString baseUrl = "http://127.0.0.1:5000/VideoDepart?Vpath=" + videoPath301;
    QUrl url(baseUrl);

    // 构造请求QNetworkRequest
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    request.setUrl(url);

    QNetworkAccessManager *manager = new QNetworkAccessManager();

    // 发送get请求
    manager->get(request);

    connect(manager, SIGNAL(finished(QNetworkReply*)), this,SLOT(dealMsg2(QNetworkReply*)));
}

void faceChange::btnPixmap302_clicked()
{
    videoPath302 = QFileDialog::getOpenFileName(this, "Open File", "E:\\Python\\FinalAIface\\test", "Image File(*.mp4;*.avi)");

    //请求视频封面
    // URL
    QString baseUrl = "http://127.0.0.1:5000/VideoDepart?Vpath=" + videoPath302;
    QUrl url(baseUrl);

    // 构造请求QNetworkRequest
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    request.setUrl(url);

    QNetworkAccessManager *manager = new QNetworkAccessManager();

    // 发送get请求
    manager->get(request);

    connect(manager, SIGNAL(finished(QNetworkReply*)), this,SLOT(dealMsg3(QNetworkReply*)));
}


//图片换脸
void faceChange::btnResult1_clicked()
{
    //发送信号
    // URL
    QString baseUrl = "http://127.0.0.1:5000/PhotoFaceSwap?Userid=" + face_user->getId() + "&Goal=" + imgPath102 + "&Origin=" + imgPath101;
    QUrl url(baseUrl);

    // 构造请求QNetworkRequest
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    request.setUrl(url);

    QNetworkAccessManager *manager = new QNetworkAccessManager();

    // 发送get请求
    manager->get(request);

    emit showWait();

    connect(manager, SIGNAL(finished(QNetworkReply*)), this,SLOT(dealMsg(QNetworkReply*)));
}

void faceChange::btnResult2_clicked()
{
    //发送信号
    // URL
    QString baseUrl = "http://127.0.0.1:5000/VideoFaceSwap?Userid=" + face_user->getId() + "&Ppath=" + imgPath201 + "&Vpath=" + videoPath202;
    QUrl url(baseUrl);

    // 构造请求QNetworkRequest
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    request.setUrl(url);

    QNetworkAccessManager *manager = new QNetworkAccessManager();

    // 发送get请求
    manager->get(request);

    emit showWait();

    connect(manager, SIGNAL(finished(QNetworkReply*)), this,SLOT(dealMsg4(QNetworkReply*)));
}

void faceChange::btnResult3_clicked()
{
    //发送信号
    // URL
    QString baseUrl = "http://127.0.0.1:5000/FaceSwap";
    QUrl url(baseUrl);

    // 构造请求QNetworkRequest
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    request.setUrl(url);

    QNetworkAccessManager *manager = new QNetworkAccessManager();

    // 发送get请求
    manager->get(request);

    //resultPath3 = "E:\\Video\\EVCapture\\demo.mp4";
    //resultPath3 = QString("file:///") + resultPath3;
    //QDesktopServices::openUrl(QUrl(resultPath3));
}

